package com.atguigu.gulimall.coupon.controller;

import com.atguigu.common.to.MemberPrice;
import com.atguigu.common.to.SkuDiscountTo;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.coupon.entity.MemberPriceEntity;
import com.atguigu.gulimall.coupon.entity.SkuFullReductionEntity;
import com.atguigu.gulimall.coupon.entity.SkuLadderEntity;
import com.atguigu.gulimall.coupon.service.MemberPriceService;
import com.atguigu.gulimall.coupon.service.SkuFullReductionService;
import com.atguigu.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("coupon/discount")
public class SkuDiscountController {

    @Autowired
    MemberPriceService memberPriceService;

    @Autowired
    SkuFullReductionService skuFullReductionService;

    @Autowired
    SkuLadderService skuLadderService;

    @PostMapping("/save")
    public R save(@RequestBody SkuDiscountTo skuDiscountTo){
        //1、保存gulimall_sms.sms_member_price
        List<MemberPrice> memberPriceList = skuDiscountTo.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntityList =
                memberPriceList
                        .stream()
                        .filter(memberPrice -> memberPrice.getPrice().compareTo(new BigDecimal(0)) == 1)
                        .map((memberPrice) -> {
                            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                            memberPriceEntity.setSkuId(skuDiscountTo.getSkuId());
                            memberPriceEntity.setMemberLevelId(memberPrice.getId());
                            memberPriceEntity.setMemberLevelName(memberPrice.getName());
                            memberPriceEntity.setMemberPrice(memberPrice.getPrice());
                            return memberPriceEntity;
                        })
                        .collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntityList);

        //2、保存gulimall_sms.sms_sku_full_reduction
        if(skuDiscountTo.getFullPrice().compareTo(new BigDecimal(0)) == 1) {
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            skuFullReductionEntity.setSkuId(skuDiscountTo.getSkuId());
            skuFullReductionEntity.setFullPrice(skuDiscountTo.getFullPrice());
            skuFullReductionEntity.setReducePrice(skuDiscountTo.getReducePrice());
            skuFullReductionEntity.setAddOther(skuDiscountTo.getPriceStatus());
            skuFullReductionService.save(skuFullReductionEntity);
        }

        //3、保存gulimall_sms.sms_sku_ladder
        if(skuDiscountTo.getFullCount() > 0) {
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            skuLadderEntity.setSkuId(skuDiscountTo.getSkuId());
            skuLadderEntity.setFullCount(skuDiscountTo.getFullCount());
            skuLadderEntity.setDiscount(skuDiscountTo.getDiscount());
            skuLadderEntity.setAddOther(skuDiscountTo.getCountStatus());

            skuLadderService.save(skuLadderEntity);
        }

        return R.ok();
    }
}
